import os
import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt

def stopwordslist():
    stopwords = [line.strip() for line in open('cn_stopwords.txt',encoding='UTF-8').readlines()]
    return stopwords


# 请替换成您的文件夹路径
folder_path = './datas_search'

# 创建一个空列表来保存所有的文本内容
all_texts = []
# 递归遍历文件夹
for root, dirs, files in os.walk(folder_path):
    for file in files:
        if file == 'detail.txt':
            file_path = os.path.join(root, file)
            with open(file_path, 'r', encoding='utf-8') as f:
                for line in f:  # 使用for line in循环读取每一行
                    if ':' in line:  # 如果该行包含冒号，则continue
                        continue
                    text = line.strip()  # 去除行首行尾的空白字符
                    all_texts.append(text)
                #print(all_texts)
# 合并所有文本内容
combined_text = ' '.join(all_texts)

stopwords = stopwordslist()

# 使用jieba进行分词
seg_list = jieba.cut(combined_text,cut_all=False)
words = [word for word in seg_list if word not in stopwords]



# 生成词云
wordcloud = WordCloud(font_path='simhei.ttf',  # 字体路径，保证中文能够显示
                      background_color='white',  # 背景颜色
                      width=800, height=600
                     ).generate(' '.join(words))

# 显示词云
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off") # 隐藏坐标轴
plt.show()

# 也可以将词云保存为图片文件
wordcloud.to_file('word_cloud.png')
